<template>
  <div class="main-layout">
    <div class="main-content">
      <h2>消息列表</h2>
      <div v-if="loading" class="loading">加载中...</div>
      <div v-else>
        <div v-if="interlocutors.length === 0" class="empty">💬 暂无会话，快去找朋友聊聊吧~</div>
        <div v-else class="conversation-list">
          <router-link
            v-for="user in interlocutors"
            :key="user.id"
            :to="{
              name: 'chatRoom',
              params: { userId: user.id },
              query: { nickname: user.nickname },
            }"
            class="conversation-item"
          >
            <img
              class="avatar"
              :src="getUserAvatar(user.photo, user.id)"
              :alt="user.nickname + '的头像'"
            />
            <span class="nickname">{{ user.nickname }}</span>
          </router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { getUserAvatar } from '@/utils/avatar'
import api from '@/services/api'

const authStore = useAuthStore()
const loading = ref(true)
const interlocutors = ref([])

// 加载聊天用户列表
async function loadInterlocutors() {
  try {
    const userId = authStore.user?.id
    if (!userId) {
      console.error('未找到用户ID')
      return
    }
    const res = await api.getInterlocutor(userId)
    if (res.data?.code === 0) {
      interlocutors.value = res.data.data
    }
  } catch (error) {
    console.error('加载聊天用户列表失败:', error)
  } finally {
    loading.value = false
  }
}

// 加载用户信息
async function loadUserInfo() {
  try {
    if (!authStore.user?.id) {
      const res = await api.getUserInfo()
      if (res.data?.code === 0) {
        authStore.user = res.data.data
      }
    }
  } catch (error) {
    console.error('加载用户信息失败:', error)
  }
}

onMounted(async () => {
  await loadUserInfo()
  await loadInterlocutors()
})
</script>

<style scoped>
.main-layout {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  background: var(--color-bg);
}
.main-content {
  width: 100%;
  max-width: 700px;
  margin: 2rem auto 0 auto;
  background: var(--color-input-bg);
  border-radius: 0.5rem;
  box-shadow: 0 2px 8px 0 rgba(0, 0, 0, 0.04);
  padding: 2rem;
  min-height: 60vh;
}
h2 {
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: 1.5rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--color-border);
}
.conversation-list {
  display: flex;
  flex-direction: column;
  gap: 0;
}
.conversation-item {
  display: flex;
  align-items: center;
  padding: 1rem 0.5rem;
  cursor: pointer;
  transition: background 0.2s;
  border-bottom: 1px solid var(--color-border);
  text-decoration: none;
}
.conversation-item:last-child {
  border-bottom: none;
}
.avatar {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 50%;
  object-fit: cover;
  margin-right: 1rem;
  border: 2px solid var(--color-primary);
}
.nickname {
  font-weight: 600;
  color: var(--color-primary);
  font-size: 1.1rem;
}
.loading {
  text-align: center;
  color: var(--color-text-secondary);
  margin-top: 2rem;
}
.empty {
  text-align: center;
  color: var(--color-text-muted);
  margin-top: 2rem;
}
</style>
